﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using System.Web.Mvc;
using Microhard.Permission.Interface;
using Microhard.Core;

namespace Microhard.Permission.Models
{
    public class PCDCache : ICacheOperator<List<PCDMOdel>>
    {
        public void RefreshCache()
        {
            IPCD ipcd = IoC.Resolve<IPCD>();
            ICacheManager cm = CacheFactory.GetCacheManager("PCD");

            var provinceData = ipcd.GetProvince();
            cm.Add(PCD.Province.ToString(), provinceData);

            var cityData = ipcd.GetCity(-1);
            cm.Add(PCD.City.ToString(), cityData);

            var distinctData = ipcd.GetDistince(-1);
            cm.Add(PCD.Distince.ToString(), distinctData);
        }

        public List<PCDMOdel> GetChild(PCD pcdType, string parentID)
        {
            ICacheManager cm = CacheFactory.GetCacheManager("PCD");
            var data = cm.GetData(pcdType.ToString()) as List<PCDMOdel>;
            if (data != null)
            {
                if (parentID == null)
                {
                    return data;
                }
                else
                {
                    return data.Where(d => d.ParentValue == parentID).ToList();
                }
            }
            return null;
        }

        public PCDMOdel GetSingle(PCD pcdType, string id)
        {
            ICacheManager cm = CacheFactory.GetCacheManager("PCD");
            var data = cm.GetData(pcdType.ToString()) as List<PCDMOdel>;
            if (data != null)
            {
                return data.Single(d => d.Value == id);
            }
            return null;
        }

        public void UpdateCache(string key, List<PCDMOdel> newModel)
        {
            throw new NotImplementedException();
        }

        public List<PCDMOdel> GetFromCache(string key)
        {
            throw new NotImplementedException();
        }

        public void RemoveFormCache(List<string> keys)
        {
            throw new NotImplementedException();
        }
    }
}
